﻿package 
{
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	
	public class AutoScroller extends MovieClip
	{
		//VARIABLES INTERNAS SETEABLES OBLIGATORIAS.
			
			public var targetClip:MovieClip;
		
		//VARIABLES INTERNAS SETEABLES OPCIONALES.
		
			public var edgeLimit:Number = 0.1;
			public var motionFactor:Number = 0.25;
			public var rollOutDelay:uint = 500;
		
		//VARIABLES INTERNAS.
		private var box:MovieClip;
		private var contenido:MovieClip;
		private var length:Number;
		private var direction:String;
		private var scrolling:Boolean = false;
		private var stopDelayTimer:Timer;
		private var mousePosition:Number;
		private var prevMousePosition:Number;
		
		//CONSTRUCTOR.
		public function AutoScroller()
		{
			
		}
		
		//INIT.
		public function init():void
		{
			//IDENTIFICA ELEMENTOS DEL TARGETCLIP.
			box = targetClip.box;
			contenido = MovieClip(targetClip.getChildByName("contentHolder"));
			
			//PRECALCULA VALORES DE SCROLLING.
			direction = contenido.width > box.width ? "horizontal" : "vertical";
			length = direction == "horizontal" ? box.width : box.height;
			
			//INICIA EL CONTROL DE SCROLLING.
			addEventListener(Event.ENTER_FRAME, scrollingControl);
		}
		
		//CONTROL DEL SCROLLING.
		private function scrollingControl(evt:Event):void
		{
			//HIT TEST CON EL BOX DEL TARGETCLIP.
			var hit:Boolean = box.hitTestPoint(targetClip.parent.mouseX, targetClip.parent.mouseY);
			
			//CALCULA LA POSICION DEL MOUSE.
			if (hit)
			{
				mousePosition = direction == "horizontal" ? targetClip.mouseX : targetClip.mouseY
				prevMousePosition = mousePosition;
			}
			else
			{
				mousePosition = prevMousePosition;
			}
			
			//CONTROLA.
			if (hit && !scrolling)
				startScrolling();
			else if (!hit && scrolling)
				delayedStopScrolling();
		}
		
		//INICIA EL SCROLLING.
		private function startScrolling():void
		{
			//REPORTA.
			scrolling = true;
			
			//SE FIJA SI HAY UN TIMER DE INTERRUPCION CORRIENDO Y LO CORTA.
			if (stopDelayTimer != null)
				stopDelayTimer.stop();
			
			//INICIA EL ENTERFRAME.
			addEventListener(Event.ENTER_FRAME, scroll);
		}
		
		//CORTA EL SCROLLING.
		private function stopScrolling(evt:TimerEvent):void
		{
			//CORTA EL ENTERFRAME.
			removeEventListener(Event.ENTER_FRAME, scroll);
		}
		
		//CORTA EL SCROLLING DESPUES DE UN TOQUE.
		private function delayedStopScrolling():void
		{
			//REPORTA.
			scrolling = false;
			
			//DISPARA EL TIMER.
			stopDelayTimer = new Timer(rollOutDelay, 1);
			stopDelayTimer.addEventListener("timer", stopScrolling);
			stopDelayTimer.start();
		}
		
		//SCROLLING.
		private function scroll(evt:Event):void
		{
			//DETERMINA EL VALOR PARCIAL DEL MOUSE (DE 0 a 1 DEPENDIENDO DE LA POSICION DEL MOUSE SOBRE EL BOX).
			var mouseParcial:Number = mousePosition / length;
			
			//CONTIENE.
			mouseParcial = mouseParcial > 1 - edgeLimit ? 1 : mouseParcial;
			mouseParcial = mouseParcial < edgeLimit ? 0 : mouseParcial;
			
			//MUEVE.
			var dif:Number;
			if (direction == "vertical")
			{
				dif = contenido.y + mouseParcial * (contenido.height - box.height);
				contenido.y -= motionFactor*dif;
			}
			else
			{
				dif = contenido.x + mouseParcial * (contenido.width - box.width);
				contenido.x -= motionFactor*dif;
			}
		}
		
		//INFO.
		public function info():void
		{
			var str:String = "AutoScroller v1.0" + String.fromCharCode(13);
			str += "---------------" + String.fromCharCode(13);
			str += "VARIABLES SETEABLES OBLIGATORIAS:" + String.fromCharCode(13);
			str += "targetClip:MovieClip" + String.fromCharCode(13);
			str += "---------------" + String.fromCharCode(13);
			str += "VARIABLES SETEABLES OPCIONALES:" + String.fromCharCode(13);
			str += "edgeLimit:Number [default = 0.1]" + String.fromCharCode(13);
			str += "motionFactor:Number [default = 0.25]" + String.fromCharCode(13);
			str += "rollOutDelay:Number [default = 500]" + String.fromCharCode(13);
			str += "---------------" + String.fromCharCode(13);
			str += "NOTAS:" + String.fromCharCode(13);
			str += "Requiere que se le asocie un clip a scrollear." + String.fromCharCode(13);
			str += "El clip a scrollear tiene que ser un ContentDisplayer, o sea, tener un contentHolder y un box como minimo." + String.fromCharCode(13);
			str += "---------------";
			trace(str);
		}
	}
	
}